package com.clw.weblogin.controller;

import com.clw.weblogin.common.entity.ResultEntity;
import com.clw.weblogin.entity.User;
import com.clw.weblogin.service.IUserService;
import com.clw.weblogin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {

    /**
     * Session存放登录用户实体类的属性名
     */
    public static final String USER_SESSION_NAME="user";

    /**
     * 自动写入注解，要在对应实现类添加@Service注解
     */
    @Autowired
    private IUserService mUserService;
    @Autowired
    private UserService userService;

    /**
     * 用户登录方法<p></p>
     * springMVC可将HTTP请求的参数封装到对应实体参数UserEntity对象中，注意请求参数名称和实体类属性名称要相同
     * @param session
     * @param user
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public ResultEntity login1(HttpSession session, User user){
        System.out.println("登录");
        System.out.println(user.toString());
        ResultEntity resultEntity=userService.login(user);
        if(200 == resultEntity.getState()){
            //登录成功，保存用户信息到Session
            session.setAttribute(USER_SESSION_NAME,resultEntity.getData());
        }
        return resultEntity;
    }





}
